{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 英伦航空公司"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from stocker import Stocker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BMI Stocker Initialized. Data covers 1984-09-07 00:00:00 to 2018-03-27 00:00:00.\n"
     ]
    }
   ],
   "source": [
    "britishmidland = Stocker('BMI')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Ex-Dividend</th>\n",
       "      <th>Split Ratio</th>\n",
       "      <th>Adj. Open</th>\n",
       "      <th>Adj. High</th>\n",
       "      <th>Adj. Low</th>\n",
       "      <th>Adj. Close</th>\n",
       "      <th>Adj. Volume</th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>Daily Change</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1984-09-07</td>\n",
       "      <td>8.63</td>\n",
       "      <td>9.00</td>\n",
       "      <td>8.63</td>\n",
       "      <td>8.75</td>\n",
       "      <td>9100.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.267890</td>\n",
       "      <td>0.279375</td>\n",
       "      <td>0.267890</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>145600.0</td>\n",
       "      <td>1984-09-07</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.003725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1984-09-10</td>\n",
       "      <td>8.75</td>\n",
       "      <td>8.75</td>\n",
       "      <td>8.75</td>\n",
       "      <td>8.75</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1984-09-10</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1984-09-11</td>\n",
       "      <td>8.88</td>\n",
       "      <td>8.88</td>\n",
       "      <td>8.88</td>\n",
       "      <td>8.88</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.275650</td>\n",
       "      <td>0.275650</td>\n",
       "      <td>0.275650</td>\n",
       "      <td>0.275650</td>\n",
       "      <td>1600.0</td>\n",
       "      <td>1984-09-11</td>\n",
       "      <td>0.275650</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1984-09-12</td>\n",
       "      <td>8.88</td>\n",
       "      <td>8.88</td>\n",
       "      <td>8.75</td>\n",
       "      <td>8.75</td>\n",
       "      <td>1600.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.275650</td>\n",
       "      <td>0.275650</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>25600.0</td>\n",
       "      <td>1984-09-12</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>-0.004035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1984-09-13</td>\n",
       "      <td>8.75</td>\n",
       "      <td>8.75</td>\n",
       "      <td>8.75</td>\n",
       "      <td>8.75</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1984-09-13</td>\n",
       "      <td>0.271615</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date  Open  High   Low  Close  Volume  Ex-Dividend  Split Ratio  \\\n",
       "0 1984-09-07  8.63  9.00  8.63   8.75  9100.0          0.0          1.0   \n",
       "1 1984-09-10  8.75  8.75  8.75   8.75     0.0          0.0          1.0   \n",
       "2 1984-09-11  8.88  8.88  8.88   8.88   100.0          0.0          1.0   \n",
       "3 1984-09-12  8.88  8.88  8.75   8.75  1600.0          0.0          1.0   \n",
       "4 1984-09-13  8.75  8.75  8.75   8.75     0.0          0.0          1.0   \n",
       "\n",
       "   Adj. Open  Adj. High  Adj. Low  Adj. Close  Adj. Volume         ds  \\\n",
       "0   0.267890   0.279375  0.267890    0.271615     145600.0 1984-09-07   \n",
       "1   0.271615   0.271615  0.271615    0.271615          0.0 1984-09-10   \n",
       "2   0.275650   0.275650  0.275650    0.275650       1600.0 1984-09-11   \n",
       "3   0.275650   0.275650  0.271615    0.271615      25600.0 1984-09-12   \n",
       "4   0.271615   0.271615  0.271615    0.271615          0.0 1984-09-13   \n",
       "\n",
       "          y  Daily Change  \n",
       "0  0.271615      0.003725  \n",
       "1  0.271615      0.000000  \n",
       "2  0.275650      0.000000  \n",
       "3  0.271615     -0.004035  \n",
       "4  0.271615      0.000000  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_history = britishmidland.stock\n",
    "stock_history.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('bmi1.txt','w')\n",
    "f.write(str(stock_history.head()))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['Adj. Close'],\n",
       " 51.6,\n",
       " Timestamp('2017-10-12 00:00:00'),\n",
       " 0.26012939967059,\n",
       " Timestamp('1984-12-11 00:00:00'),\n",
       " 46.1,\n",
       " Timestamp('2018-03-27 00:00:00'))"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "britishmidland.plot_stock()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('bmi2.txt','w')\n",
    "f.write(str(britishmidland.plot_stock()))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['Daily Change'],\n",
       " 3.4762929939819998,\n",
       " Timestamp('2007-08-16 00:00:00'),\n",
       " -2.809054606928999,\n",
       " Timestamp('2008-10-16 00:00:00'),\n",
       " -0.7999999999999972,\n",
       " Timestamp('2018-03-27 00:00:00'))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "britishmidland.plot_stock(start_date = '2000-01-03', end_date = '2018-01-16', \n",
    "                     stats = ['Daily Change'], plot_type='pct')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('bmi3.txt','w')\n",
    "f.write(str(britishmidland.plot_stock(start_date = '2000-01-03', end_date = '2018-01-16', \n",
    "                     stats = ['Daily Change'], plot_type='pct')))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['Adj. Volume'],\n",
       " 3819600.0,\n",
       " Timestamp('2009-06-23 00:00:00'),\n",
       " 0.0,\n",
       " Timestamp('2000-01-26 00:00:00'),\n",
       " 102292.0,\n",
       " Timestamp('2018-03-27 00:00:00'))"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "britishmidland.plot_stock(start_date = '2000-01-03', end_date = '2018-01-16', \n",
    "                     stats = ['Adj. Volume'], plot_type='pct')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('bmi4.txt','w')\n",
    "f.write(str(britishmidland.plot_stock(start_date = '2000-01-03', end_date = '2018-01-16', \n",
    "                     stats = ['Adj. Volume'], plot_type='pct')))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('BMI',\n",
       " Timestamp('1986-03-13 00:00:00'),\n",
       " Timestamp('2018-01-16 00:00:00'),\n",
       " 100,\n",
       " 4880.705789179369)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "britishmidland.buy_and_hold(start_date='1986-03-13', end_date='2018-01-16', nshares=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('bmi5.txt','w')\n",
    "f.write(str(britishmidland.buy_and_hold(start_date='1986-03-13', end_date='2018-01-16', nshares=100)))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('BMI',\n",
       " Timestamp('1999-01-05 00:00:00'),\n",
       " Timestamp('2002-01-03 00:00:00'),\n",
       " 100,\n",
       " -98.25278265136998)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "britishmidland.buy_and_hold(start_date='1999-01-05', end_date='2002-01-03', nshares=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('bmi6.txt','w')\n",
    "f.write(str(britishmidland.buy_and_hold(start_date='1999-01-05', end_date='2002-01-03', nshares=100)))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Downloads\\Anaconda\\lib\\site-packages\\pystan\\misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  elif np.issubdtype(np.asarray(v).dtype, float):\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Adj. Close</th>\n",
       "      <th>delta</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>193</th>\n",
       "      <td>2016-01-04</td>\n",
       "      <td>27.826092</td>\n",
       "      <td>1.219656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>602</th>\n",
       "      <td>2017-08-17</td>\n",
       "      <td>43.872832</td>\n",
       "      <td>-1.172889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>385</th>\n",
       "      <td>2016-10-06</td>\n",
       "      <td>33.303523</td>\n",
       "      <td>1.087876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>289</th>\n",
       "      <td>2016-05-20</td>\n",
       "      <td>35.416948</td>\n",
       "      <td>-0.854542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>530</th>\n",
       "      <td>2017-05-05</td>\n",
       "      <td>38.325861</td>\n",
       "      <td>0.737674</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Date  Adj. Close     delta\n",
       "193 2016-01-04   27.826092  1.219656\n",
       "602 2017-08-17   43.872832 -1.172889\n",
       "385 2016-10-06   33.303523  1.087876\n",
       "289 2016-05-20   35.416948 -0.854542\n",
       "530 2017-05-05   38.325861  0.737674"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "britishmidland.changepoint_date_analysis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Downloads\\Anaconda\\lib\\site-packages\\pystan\\misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  elif np.issubdtype(np.asarray(v).dtype, float):\n"
     ]
    }
   ],
   "source": [
    "f = open('bmi7.txt','w')\n",
    "f.write(str(britishmidland.changepoint_date_analysis()))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Downloads\\Anaconda\\lib\\site-packages\\pystan\\misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  elif np.issubdtype(np.asarray(v).dtype, float):\n"
     ]
    }
   ],
   "source": [
    "model, future = britishmidland.create_prophet_model(days=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Downloads\\Anaconda\\lib\\site-packages\\pystan\\misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  elif np.issubdtype(np.asarray(v).dtype, float):\n"
     ]
    }
   ],
   "source": [
    "f = open('bmi8.txt','w')\n",
    "f.write(str(britishmidland.create_prophet_model(days=30)))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
